<template>
  <!-- 条件栏 -->
  <page-filter
    v-model:query="filterInfo.query"
    :filter-list="filterInfo.list"
  />
  <DialogTable
    ref="resourceTable"
    className="reprotTable"
    :refresh="tableInfo.refresh"
    :init-curpage="tableInfo.initCurpage"
    v-model:data="tableInfo.data"
    :api="api"
    :listen-height="false"
    :reserve-selection="true"
    :rowKey="rowKey"
    v-model:query="filterInfo.query"
    :field-list="tableInfo.fieldList"
    :handle="tableInfo.handle"
    :highlight-row="highlightRow"
    :check-box="checkBox"
    :checkedRowList="checkedRowList"
    @handleEvent="handleEvent"
  />
</template>

<script setup name="SelectCargoSpaceReport">
import { goodsLocationPage } from '@/api/wms.js'
import { getDepotAll } from '@/api/core'

const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: goodsLocationPage,
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '选择货位',
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  showDepot: {
    type: Boolean,
    default: true,
  },
})
const filterInfo = reactive({
  query: {
    sort: 'create_time desc',
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '货位编号',
      value: 'locationNoLike',
      event: search,
    },
    {
      label: '仓库',
      value: 'depotId',
      showType: 1,
      query: {
        depotAuth: 'id',
        status: 1,
      },
      show: props.showDepot,
      api: getDepotAll,
      type: 'slip-select',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})

const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '货位编号', value: 'locationNo' },
    { label: '货架编号', value: 'shelfNumber' },
    { label: '仓库', value: 'depotName' },
  ],
})

const selectRow = ref('')
// 获取列表
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
window.addEventListener('message', receiveMessageIframePage, false)
function receiveMessageIframePage(data) {
  let res = data.data
  // 重置选中
  if (res.type === 'customDialogReset') {
    tableInfo.initCurpage = Math.random()
    proxy.$refs.resourceTable.clearSelection()
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      handelClickRow(data)
      selectRow.value = data
      break
    case 'tableCheck':
      handelClickRow(data)
      selectRow.value = data
      break
  }
}
function handelClickRow(data) {
  window.parent.postMessage(
    { type: 'tableClickRow', data: handleCheckData(data) },
    '*'
  )
}
function handleCheckData(data) {
  return data.map((element) => {
    return {
      label: element.locationNo,
      value: element.id,
    }
  })
}

onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})
</script>

<style scoped lang="scss">
.el-dialog__body {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
